package cn.com.pansky.action.myaccount;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import com.alibaba.fastjson.JSONObject;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.exception.Url2SqlErrorException;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import cn.com.pansky.action.imgContrast.ImgContrast;
import cn.com.pansky.service.CommonService;
import cn.com.pansky.util.LssConstant;

public class RefreshFaceAction extends CommonAction {

	CommonService cs = new CommonService();

	public void refreshFace() {
		String CA001 = getParam("CA001");
		// 获取刷脸图片
		String image_best = context.getParam("image_best");
		String image_env = context.getParam("image_env");
		String delta = context.getParam("delta");
		String filename = "";
		try {
			Properties p = new Properties();
			p.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
			// 指定身份证照片保存路径：
			filename = p.getProperty("imgPath") + CA001 + ".jpg";
			cs.getIdcardPic(CA001, filename);
			// 调用照片比对方法，比对刷脸与身份证照片
			JSONObject result = ImgContrast.imgCompare(filename, image_best, image_env, delta);
			if(0 != result.getIntValue("code")){
				datas.put("isPass", false);
				datas.put("info", "请确认身份证与刷脸是同一人！");
			}else{
				double num = result.getDoubleValue("confidence");
				// 判断照片相似度
				if (num > LssConstant.CONFIDENCE) {
					
					// 判断库中是否有这个人的特征信息：
					String findFeatures = urlConfig.getVar("findFeatures");
					Model m = SqlHelper.queryOne(findFeatures, CA001);
					String id = "";
					if (m != null) {
						id = m.getString("CA011");
					}
					cs.isnertRC06(image_best, CA001, num, id);
					datas.put("isPass", true);
					datas.put("info", "刷脸成功");
				} else {
					datas.put("isPass", false);
					datas.put("info", "与证件照片相似度过低！");
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new Url2SqlErrorException(-1, "服务异常，请联系管理员");
		} catch (IOException e) {
			e.printStackTrace();
			throw new Url2SqlErrorException(-1, "服务异常，请联系管理员");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Url2SqlErrorException(-1, "服务异常，请联系管理员");
		} finally {
			// 将身份证临时照片删除
			File file = new File(filename);
			file.delete();
			// 将我要认证刷脸图片删
			File file2 = new File(image_best);
			file2.delete();
			// 将我要认证刷脸图片删
			File file3 = new File(image_env);
			file3.delete();
		}
		context.getDatas().put("result", datas);
	}
}
